/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"9B867C93-592A-4440-867E-54532C8CD76C",variableType:4}
 */
var $historico = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"F4876429-DBCD-4F10-87B9-2540AA62CC58",variableType:4}
 */
var $historico_listado_roles = 1;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"6B40F8B2-6719-4415-BF47-A5690265A993",variableType:4}
 */
var $historico_listado_usuarios = 2;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"A18A3886-15CB-4F2A-8D2A-38ED68B99F67",variableType:4}
 */
var $historico_listado_menus_por_perfil = 3;

/**
 * @param {JSEvent} event
 *
 * @properties={typeid:24,uuid:"B7BCABA4-C446-4A65-AA11-CCFA74D12DE7"}
 */
function opciones(event){
	
	var menu = plugins.window.createPopupMenu();
	menu.addMenuItem("Generar Roles", generar_roles);

	if (event.getSource()) {
		menu.show(event.getSource());
	}
}

/**
 * @properties={typeid:24,uuid:"18597E13-D962-46C7-9623-BDF897537E39"}
 */
function generar_roles(){
	
	scopes.adm_t900_roles.generarRoles(foundset.getSelectedRecord());
}

/**
 * Hide btn reset fields
 * @author Roberto J. Blasco
 * @version 1.0
 * @private 
 * @properties={typeid:24,uuid:"E9606ABB-ACAC-48D8-BCC6-F07354267977"}
 */
function hide_btn_reset_fields(){
	
	elements.btn_ir_empresa.visible = true;
}

/**
 * doEdit
 * @author Roberto J. Blasco
 * @version 1.0
 * @private 
 * @properties={typeid:24,uuid:"E7797FD8-995F-4A17-8456-BF5DD5B6C952"}
 */
function doEdit(){
	
	if (adm_t900_empresas_id != null){
		globals.hide_btn_reset_fields_elment(controller.getName(),elements.vlst_adm_t900_empresas_id.getName());
	}
	
	elements.btn_ir_empresa.visible = false;
	controller.focusField(elements.fld_descripcion.getName(),true);
}

/**
 * Callback method for when form is shown.
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 * @private
 * @properties={typeid:24,uuid:"73D6D78C-84C1-4D15-9630-8300F255F702"}
 */
function onShow(firstShow, event) {

	if (foundset.getSelectedRecord().isNew()){
		elements.grupo_historicos.visible = false;
		s_descripcion = adm_t900_perfiles_to_adm_t900_empresas.s_nombre_corto + "@";
	}else{
		
		elements.grupo_historicos.visible = true;
		var dataset = databaseManager.createEmptyDataSet(0,new Array('display_values','optional_real_values'));
		dataset.addRow(['LISTADO DE ROLES', $historico_listado_roles]);
		dataset.addRow(['LISTADO DE USUARIOS', $historico_listado_usuarios]);
		dataset.addRow(['LISTADO DE MENÚS POR PERFIL', $historico_listado_menus_por_perfil]);
		elements.historicos.setValueListItems(dataset);
		
		elements.tab_historicos.visible = true;
		elements.tab_historicos.removeAllTabs();
		elements.tab_historicos.addTab(forms.lst_adm_t900_roles_h,null,null,null,null,null,null,adm_t900_perfiles_to_adm_t900_roles);
		$historico = $historico_listado_roles;
	}
}

/**
 * Callback method when form is (re)loaded.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"F3FC9389-0964-4C50-9E38-F6C9A8769562"}
 * @AllowToRunInFind
 */
function onLoad(event) {

	
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @private
 *
 * @properties={typeid:24,uuid:"F410DA3E-B11B-446B-AE89-3991AC33C4F8"}
 */
function onDataChangeHistorico(oldValue, newValue, event) {

	elements.tab_historicos.removeAllTabs();
	if ($historico == $historico_listado_roles){
		elements.tab_historicos.addTab(forms.lst_adm_t900_roles_h,null,null,null,null,null,null,adm_t900_perfiles_to_adm_t900_roles);
	}else if ($historico == $historico_listado_usuarios){
		elements.tab_historicos.addTab(forms.lst_adm_t900_usuarios_h,null,null,null,null,null,null, adm_t900_perfiles_to_adm_t900_usuarios);
	}else if ($historico == $historico_listado_menus_por_perfil){
		elements.tab_historicos.addTab(forms.lst_adm_t300_perfiles_menus__menus_h,null,null,null,null,null,null, adm_t900_perfiles_to_adm_t300_perfiles_menus);
	}
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"7A04E1E4-C468-4432-A3D0-A5180AE0CE84"}
 */
function ir_empresa(event) {
	
	/** @type {JSFoundSet<db:/pigeon_board/adm_t900_empresas>} */
	var fs_adm_t900_empresas = databaseManager.getFoundSet(scopes.adm_t900_empresas.$DATA_SOURCE);
	fs_adm_t900_empresas.loadRecords(adm_t900_empresas_id);
	globals.ir_formulario(forms.frm_adm_t900_empresas.controller.getName(),fs_adm_t900_empresas,globals.$MODO_LECTURA);
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"AF3AB98A-FFA7-4A3A-976E-4C9C8565FD92"}
 */
function opciones_historico(event) {
	
	var menu = plugins.window.createPopupMenu();
	if ($historico == $historico_listado_roles){
	}else if ($historico == $historico_listado_usuarios){
	}else if ($historico == $historico_listado_menus_por_perfil){
		var submenu_menus_por_perfil_recalcular_menus = menu.addMenuItem("Recalcular Menús", menus_por_perfil_recalcular_menus);
		submenu_menus_por_perfil_recalcular_menus.setIcon("media:///iconos/ic_autorenew_grey600_18dp.png");
	}
	
	if (event.getSource()) {
		menu.show(event.getSource());
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"CBBA51F1-5C73-4D7F-A463-FC958C46E0C4"}
 */
function alta_historico(event) {
	// TODO Auto-generated method stub
}

/**
 * @properties={typeid:24,uuid:"92289104-D443-4E44-9EEC-D3E8C1CC24B2"}
 */
function menus_por_perfil_recalcular_menus(){
	
	foundset.getSelectedRecord().adm_t900_perfiles_to_adm_t300_perfiles_menus.deleteAllRecords();
	/** @type {JSFoundSet<db:/pigeon_board/adm_t900_menus>} */
	var fs_adm_t900_menus = databaseManager.getFoundSet(scopes.adm_t900_menus.$DATA_SOURCE);
	fs_adm_t900_menus.loadAllRecords();
	/** @type {JSFoundSet<db:/pigeon_board/adm_t300_perfiles_menus>} */
	var fs_adm_t300_perfiles_menus = databaseManager.getFoundSet(scopes.adm_t300_perfiles_menus.$DATA_SOURCE);
	for (var x = 1; x <= databaseManager.getFoundSetCount(fs_adm_t900_menus); x++){
		fs_adm_t300_perfiles_menus.newRecord();
		fs_adm_t300_perfiles_menus.adm_t900_perfiles_id = foundset.getSelectedRecord().adm_t900_perfiles_id;
		fs_adm_t300_perfiles_menus.adm_t900_menus_id = fs_adm_t900_menus.getRecord(x).adm_t900_menus_id;
	}
	databaseManager.saveData(fs_adm_t900_menus);
	
}
